package com.easpeed.comon.socket_tool

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ClipboardUtils
import com.easpeed.elevator.R
import com.easpeed.elevator.utils.WwAdapter
import com.easpeed.elevator.utils.WwViewHolder

class SocketInfoActivity : AppCompatActivity() {


    private lateinit var mContext: Context

    val socketServer = SocketServer()

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mContext = this
        setContentView(R.layout.activity_socket_info)



        setText(R.id.socket_ip, "本机ip地址：${SocketTools.getLocalIpAddress()}")

        setText(R.id.ip_port_input, "${SocketTools.getLocalIpAddress()}:12345")
        setText(R.id.ip_port_show, "12345")
        iniRecyclerview(getView(R.id.recylerview))

        setClick(R.id.socket_client_btn) {
            setVisibility(R.id.client_layout, View.VISIBLE)
            setVisibility(R.id.server_layout, View.GONE)
        }
        setClick(R.id.socket_server_btn) {
            setVisibility(R.id.client_layout, View.GONE)
            setVisibility(R.id.server_layout, View.VISIBLE)
        }

        // 点击启动客户端
        setClick(R.id.start_connect) {
            if (SocketClient.instance.isConnected()) {
                SocketClient.instance.stopClient()
            } else {
                setEnabled(R.id.socket_server_btn, false)
                setEnabled(R.id.socket_client_btn, false)
                setEnabled(R.id.ip_port_input, false)
                setEnabled(R.id.ip_port_show, false)
                SocketClient.instance.startClient(getTextStr(R.id.ip_port_input))
            }
        }

        // 点击启动服务端
        setClick(R.id.start_server) {
            if (socketServer.isServerStart()) {
                socketServer.stopServer()
            } else {
                setEnabled(R.id.socket_server_btn, false)
                setEnabled(R.id.socket_client_btn, false)
                setEnabled(R.id.ip_port_input, false)
                setEnabled(R.id.ip_port_show, false)
                socketServer.startServer(getTextStr(R.id.ip_port_show))
            }
        }
        socketServer.apply {
            // 接受到消息
            setOnMessageListener {
                addMessage(it)
            }
            //服务端启动监听
            setServerStartListener { isStart, msg ->
                if (isStart) {
                    setText(R.id.start_server, "停止服务")

                } else {
                    setText(R.id.start_server, "启动服务")
                    setEnabled(R.id.socket_server_btn, true)
                    setEnabled(R.id.socket_client_btn, true)
                    setEnabled(R.id.ip_port_input, true)
                    setEnabled(R.id.ip_port_show, true)
                }
            }
        }


        // 客户端连接监听
        SocketClient.instance.apply {
            // 接受到消息
            setOnMessageListener {
                addMessage(it)
            }
            this.setOnConnectListener {
                if (it) {
                    setText(R.id.start_connect, "断开连接")
                } else {
                    setText(R.id.start_connect, "连接服务器")
                    setEnabled(R.id.socket_server_btn, true)
                    setEnabled(R.id.socket_client_btn, true)
                    setEnabled(R.id.ip_port_input, true)
                    setEnabled(R.id.ip_port_show, true)
                }
            }
        }
        if (SocketClient.instance.isConnected()) {
            getView<View>(R.id.socket_client_btn).performClick()
            setText(R.id.ip_port_input, "${SocketClient.instance.getUrl()}")
            setText(R.id.start_connect, "断开连接")
            setEnabled(R.id.socket_server_btn, false)
            setEnabled(R.id.socket_client_btn, false)
            setEnabled(R.id.ip_port_input, false)
            setEnabled(R.id.ip_port_show, false)
        }
        if (socketServer.isServerStart()) {
            getView<View>(R.id.socket_server_btn).performClick()
            setText(R.id.ip_port_show, socketServer.getPort())
            setText(R.id.start_server, "停止服务")
            setEnabled(R.id.socket_server_btn, false)
            setEnabled(R.id.socket_client_btn, false)
            setEnabled(R.id.ip_port_input, false)
            setEnabled(R.id.ip_port_show, false)
        }
    }


    private var wwAdapterOptical: WwAdapter<String>? = null
    private var tempList: ArrayList<String> = arrayListOf()


    @SuppressLint("NotifyDataSetChanged")
    private fun addMessage(message: String?) {
        if (message == null) return
        tempList.add(message)
        wwAdapterOptical?.notifyDataSetChanged()
    }

    private fun iniRecyclerview(recyclerView: RecyclerView) {
        wwAdapterOptical = object : WwAdapter<String>() {

            override fun initRecyclerView(mContext: Context, adapter: WwAdapter<String>) {
                val manager = LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)
                recyclerView.layoutManager = manager
                recyclerView.adapter = adapter
            }

            override fun getLayoutId(): Int {
                return R.layout.item_socket_info
            }

            @SuppressLint("NotifyDataSetChanged")
            override fun convert(holder: WwViewHolder, bean: String, position: Int) {
                holder.setText(R.id.content, bean)
                holder.setOnClickListener(R.id.content) {
                    ClipboardUtils.copyText(bean)
                }
                holder.setOnLongClickListener(R.id.content) {
                    tempList.subList(0, position).clear()
                    wwAdapterOptical?.notifyDataSetChanged()
                    return@setOnLongClickListener true
                }
            }
        }.create(mContext, tempList)
    }

    fun <T : View> getView(id: Int): T {
        return findViewById<T>(id)
    }

    fun setClick(id: Int, viewClick: (view: View) -> Unit) {
        getView<View>(id).setOnClickListener {
            viewClick(getView(id))
        }
    }

    fun setText(id: Int, text: String) {
        getView<TextView>(id).text = text
    }

    fun getTextStr(id: Int): String {
        return getView<TextView>(id).text.toString()
    }

    fun setEnabled(id: Int, enabled: Boolean) {
        getView<View>(id).isEnabled = enabled
    }

    fun setVisibility(id: Int, visibility: Int) {
        getView<View>(id).visibility = visibility
    }
}